
package uestc.bitman.edxapp.downloading;

import uestc.bitman.edxapp.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("DefaultLocale")
public class DownloadingListItem {

    public static int STATUS_DOWNLOADING = 0;

    public static int STATUS_PAUSE = 1;

    public static int STATUS_OK = 2;

    public static int STATUS_ERROR = -1;

    private ProgressBar mProgressBar;

    private TextView mStatusView;

    private TextView mNameView;

    private View mItemView;

    private DownloadTaskThread mDownloadingTask = null;

    private Handler mFinishHandler = null;

    private DownloadingTaskBreakPoints mBreakPoints = null;

    private Context mContext;

    private String mSectionName, mCourseName, mChapterName, mVideoTitle;

    IDownloadingCallback mCallback = new IDownloadingCallback() {
        @Override
        public void onProgress(int progress, int speed) {
            setStatus(progress, speed);
        }

        @Override
        public void onDownloadStart(int filesize) {
            Toast.makeText(mContext, mContext.getString(R.string.label_start_download),
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onDownloadEnd(int progress) {
            if (progress == 100) {
                if (mFinishHandler != null) {
                    Message msg = Message.obtain();
                    msg.what = DownloadTaskThread.MSG_DOWNLOAD_FINISH;
                    msg.obj = DownloadingListItem.this;
                    mFinishHandler.sendMessage(msg);
                }
            } else {
                mStatusView.setText(mContext.getString(R.string.label_download_pause));
            }
        }

        @Override
        public void onNoConnectionError(int code) {
            // no Internet connection
            mBreakPoints = null;
            mDownloadingTask.stopDownload();
            Toast.makeText(mContext, mContext.getString(R.string.label_connection_failure),
                    Toast.LENGTH_SHORT).show();
            mStatusView.setText(mContext.getString(R.string.label_connection_failure));
        }

        @Override
        public void onDownloadInterrupted(int code) {
            // download inteerrupted
            mBreakPoints = mDownloadingTask.getBreakPoints();
            mDownloadingTask.stopDownload();
            Toast.makeText(mContext, mContext.getString(R.string.label_connection_interrupt),
                    Toast.LENGTH_SHORT).show();
        }
    };

    public DownloadingListItem(Context context, DownloadTaskThread fileDownloadTask,
            String sectionName, String chapterName, String courseName, String videoTitle) {

        this.mContext = context;
        this.mSectionName = sectionName;
        this.mCourseName = courseName;
        this.mChapterName = chapterName;
        this.mVideoTitle = videoTitle;

        mItemView = LayoutInflater.from(context).inflate(R.layout.download_item, null);
        mItemView.setTag(this);
        mNameView = (TextView) mItemView.findViewById(R.id.dl_name);
        View subView = mItemView.findViewById(R.id.dl_status_view);
        mProgressBar = (ProgressBar) subView.findViewById(R.id.dl_progress);
        mStatusView = (TextView) subView.findViewById(R.id.dl_status);

        mDownloadingTask = fileDownloadTask;
        mDownloadingTask.setTaskCallback(mCallback);

        setProgressBar(0);
        mStatusView.setText(context.getString(R.string.label_start_download));
        mNameView.setText(courseName + "_" + videoTitle);

    }

    public String getChapterName() {
        return mSectionName;
    }

    public String getCourseName() {
        return mCourseName;
    }

    public String getSectionName() {
        return mChapterName;
    }

    public String getVideoTitle() {
        return mVideoTitle;
    }

    public void toggle() {
        if (mDownloadingTask.mStop) {
            if (mBreakPoints != null) {
                mDownloadingTask.loadBreakPoints(mBreakPoints, mCallback);
                mCourseName = mBreakPoints.courseName;
                mSectionName = mBreakPoints.sectionName;
            }
            mDownloadingTask.startDownload();
        } else {
            mBreakPoints = mDownloadingTask.getBreakPoints();
            mBreakPoints.sectionName = mSectionName;
            mBreakPoints.courseName = mCourseName;
            mDownloadingTask.stopDownload();
        }
    }

    public View getItemView() {
        return mItemView;
    }

    public void setDownloadFinishHandler(Handler handler) {
        this.mFinishHandler = handler;
    }

    public void startDownloadTask() {
        mDownloadingTask.startDownload();
    }

    public void pauseDownload() {
        mBreakPoints = mDownloadingTask.getBreakPoints();
        mDownloadingTask.stopDownload();
    }

    public void cancelDownload() {
        mDownloadingTask.cancelDownload();
    }

    public void resumeDownload() {
        if (mBreakPoints != null) {
            mDownloadingTask.loadBreakPoints(mBreakPoints, mCallback);
        }
        mDownloadingTask.startDownload();
    }

    public DownloadTaskThread getFileDownloadTask() {
        return mDownloadingTask;
    }

    public void setStatus(int progress, int speed) {
        String s = progress + "%" + "     ";
        if (speed <= 1000 * 1024) {
            s += (speed / 1024) + " Kb/s";
        } else {
            s += String.format("%.2f Mb/s", (float) speed / (float) (1024 * 1024));
        }
        mStatusView.setText(s);
        setProgressBar(progress);
    }

    public void setProgressBar(int progress) {
        mProgressBar.setProgress(progress);
    }
}
